//index.js
//获取应用实例
const app = getApp();
import {
    getBluetoothDevices,
    createBLEConnection,
    closeBLEConnection,
    openBluetoothAdapter,
    closeBluetoothAdapter,
    startBluetoothDevicesDiscovery,
    stopBluetoothDevicesDiscovery,
    getBluetoothAdapterState
} from './wxPromise'

Page({
    data: {
        devices: [],
        deviceid: "A0120281-C34C-D7C6-EA8F-EF569CD48891"
    },

    async onReady() {
    },
    async handleOpen() {
        try {
            let res = await openBluetoothAdapter()
            console.log(res)
            await this.handleStartSearchDevice()
        } catch (e) {
            console.log(e)
        }
    },
    //获取本机蓝牙适配器状态
    async getBluetoothAdapterState() {
        try {
            let res = await getBluetoothAdapterState()
            console.log(res)
        } catch (e) {
            console.log(e)
        }
    },
    //开始搜索设备
    async handleStartSearchDevice() {
        try {
            let res = await startBluetoothDevicesDiscovery()
            console.log(res)
        } catch (e) {
            console.log(e)
        }
    },
    //停止搜索设备
    async handleStopSearchDevice() {
        try {
            let res = await stopBluetoothDevicesDiscovery()
            console.log(res)
        } catch (e) {
            console.log(e)
        }
    },
    //获取搜索到设备
    getFoundDevice() {
        wx.onBluetoothDeviceFound(({devices}) => {
            console.log(devices)
            let device = devices[0]
            if (device.deviceId === "4C28560C-B573-E1FC-A8E5-AA5D4D4DD50D") {
                this.handleStopSearchDevice()
            }
        })
    },
    //获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
    async getBluetoothDevices() {
        try {
            let devices = await getBluetoothDevices()
            this.setData({
                devices
            })
        } catch (e) {
            console.log(e)
        }
    },
    //开始链接设备
    async handleConnection() {
        try {
            let res = await createBLEConnection()
            console.log(res)
        } catch (e) {
            console.log(e)
        }
    },
    //断开链接
    async handleCloseBLEConnection() {
        try {
            let res = await closeBLEConnection()
            console.log(res)
        } catch (e) {
            console.log(e)
        }
    },
    //关闭蓝牙
    async handleOff() {
        try {
            let res = await closeBluetoothAdapter()
            console.log(res)
        } catch (e) {
            console.log(e)
        }
    },
    async onLoad() {
        try {
            let data = wx.getBatteryInfoSync()
            this.handleOpen()
            //监听蓝牙适配器状态变化事件
            wx.onBluetoothAdapterStateChange((res) => {
                /*
                * @params available  是否可用
                * @params discovering 是否处于搜索状态
                * */
                if (res.available && res.discovering) {
                    this.getFoundDevice()
                } else if (res.available && !res.discovering) {
                    this.getBluetoothDevices()
                }
                console.log('adapterState changed, now is', res)
            })
            //监听低功耗蓝牙连接状态的改变事件。包括开发者主动连接或断开连接，设备丢失，连接异常断开等等
            wx.onBLEConnectionStateChange(function (res) {
                // 该方法回调中可以用于处理连接意外断开等异常情况
                console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
            })
        }catch (e) {
            console.log(e)
        }


    }
});
